﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Gman.Entity.Entities;
using Gman.WebApp.ServiceControllers;
using Gman.WebApp.Models;
using Gman.WebApp.Controllers.Base;

namespace Gman.WebApp.Controllers
{
    public class PersonalController : Controller
    {
        private OperacionServiceController service = new OperacionServiceController();  

        public PersonalModel GetPersonalModel(Guid id)
        {
            var cache = new CacheProvider();

            string key = id.ToString();
            var existingModel = cache.Get(key);
            if (existingModel == null)
            {
                existingModel = new PersonalModel(new PersonalBE());
                cache.Set(key, existingModel);
            }
            return (PersonalModel)existingModel;
        }

        public ActionResult UpdateField(string fieldName, string fieldValue, Guid id, string setterFormatOptions)
        {
            PersonalModel model = GetPersonalModel(id);
            SetterFormatOptions setterFormatOptionsEnum = SetterFormatOptionsHelper.Parse(setterFormatOptions);
            List<KeyValuePair<string, string>> validationList = new List<KeyValuePair<string, string>>();

            var setter = new PropertySetter<PersonalModel>.SetterOptions();
            setter.objectToSet = model;
            setter.property = fieldName;
            setter.val = fieldValue;
            setter.SetterFormatOptions = setterFormatOptionsEnum;
            setter.validationList = validationList;

            object valueWasSet = PropertySetter<PersonalModel>.SetFinalObject(setter);
            validationList.AddRange(new PersonalModelValidator().Validate(model));

            return Json
                (new
                {
                    result = true,
                    PropertyValidationList = validationList,
                }
                );
        }

        public ActionResult GetValidation(Guid id)
        {
            PersonalModel model = GetPersonalModel(id);
            List<KeyValuePair<string, string>> validationList = new List<KeyValuePair<string, string>>();            

            validationList.AddRange(new PersonalModelValidator().Validate(model));

            return Json(new
            {
                PropertyValidationList = validationList,                
            });
        }
    }
}
